package com.xadmin.sys.rbac.controller;


import com.xadmin.sys.common.response.Result;
import com.xadmin.sys.common.utils.Page;
import com.xadmin.sys.rbac.mapper.MenuMapper;
import com.xadmin.sys.rbac.pojo.Menu;
import com.xadmin.sys.rbac.service.MenuService;
import com.xadmin.sys.rbac.vo.MenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.xadmin.sys.rbac.utils.Convert.convertToMenuVo;
import static com.xadmin.sys.rbac.utils.FindChild.findMenuChild;
import static com.xadmin.sys.rbac.utils.FindRoot.findMenuRoot;

/**
 * 系统菜单
 */
@RestController
@RequestMapping("/menu")
@Api(value = "系统菜单模块",tags = "系统菜单接口")
public class MenuController {
    @Resource
    private MenuService menuService;
    @Resource
    private MenuMapper menuMapper;

    @PreAuthorize("hasRole('SUPERADMIN')")
    @RequestMapping(value = "/menuList",method = RequestMethod.POST)
    @ApiOperation(value = "分页查询菜单信息",notes = "分页查询菜单信息")
    public Result getByPage(@RequestBody Page<Menu> page){
        page = menuService.getAllByPage(page);
        return  Result.ok().data("data",page);
    }

    @PreAuthorize("hasRole('SUPERADMIN')")
    @RequestMapping(value = "/getAllMenu",method = RequestMethod.GET)
    @ApiOperation(value = "查询所有菜单信息",notes = "查询所有菜单信息")
    public Result getAllMenu(){
        List<Menu> menuList = menuMapper.selectAll();
        /*将menuList转换成menuVoList*/
        List<MenuVo> menuVoList = convertToMenuVo(menuList);
        /*找根节点*/
        List<MenuVo> rootMenu = findMenuRoot(menuVoList);
        /*为根节点设置子菜单*/
        for(MenuVo menuVo : rootMenu){
            /*获取某个根节点的子菜单*/
            List<MenuVo> childList = findMenuChild(menuVo.getId(),menuVoList);
            menuVo.setChildren(childList);
        }
        List<Integer> ids = menuService.findOpenIds();
        Map<String, Object> map = new HashMap<>();
        map.put("tree", rootMenu);
        map.put("open", ids);
        return  Result.ok().data("data",map);
    }


    @PreAuthorize("hasRole('SUPERADMIN')")
    @RequestMapping(value = "/addMenu",method = RequestMethod.POST)
    @ApiOperation(value = "添加菜单",notes = "添加菜单")
    public Result addMenu(@RequestBody Menu menu){
        boolean  check =menuService.save(menu);
        return  Result.ok().message("添加成功！");
    }

    @PreAuthorize("hasRole('SUPERADMIN')")
    @RequestMapping(value = "/deleteMenu/{id}",method = RequestMethod.DELETE)
    @ApiOperation(value = "根据ID删除菜单",notes = "根据ID删除菜单")
    public Result deleteMenu(@PathVariable Integer id){
        boolean check = menuService.removeById(id);
        if (check == true){
            return  Result.ok().message("删除成功！");
        }else{
            return Result.error().message("删除失败！");
        }
    }

    @PreAuthorize("hasRole('SUPERADMIN')")
    @RequestMapping(value = "/viewMenu/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "根据ID查看用户菜单",notes = "根据ID查看用户菜单")
    public Result viewMenu(@PathVariable Integer id){
        Menu menu = menuService.getById(id);
        return  Result.ok().data("data",menu);
    }

    @PreAuthorize("hasRole('SUPERADMIN')")
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    @ApiOperation(value = "根据ID更新用户菜单",notes = "根据ID更新用户菜单")
    public Result update(@RequestBody Menu menu){
        boolean check = menuService.updateById(menu);
        if (check == true){
            return  Result.ok().message("更新成功！");
        }else{
            return Result.error().message("更新失败！");
        }
    }

}

